package com.example.backend.service;

import com.example.backend.util.IpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Service
public class DataService {

    @Autowired
    private JdbcTemplate messageJdbcTemplate;

    @Autowired
    private JdbcTemplate gameMessageJdbcTemplate;

    public Map<String, Object> getUserData(HttpServletRequest request) {
        // 获取客户端IP地址
        String ip = IpUtils.getClientIp(request);
        System.out.println(ip);

        // Step 1: Query `message` database
        String queryUserSQL = "SELECT username FROM user WHERE ip = ?";
        List<Map<String, Object>> userResult = messageJdbcTemplate.queryForList(queryUserSQL, ip);

        if (userResult.isEmpty()) {
            throw new RuntimeException("User not found for IP: " + ip);
        }

        String username = (String) userResult.get(0).get("username");

        // Step 2: Query `game_message` database for user data
        String queryGameDataSQL = "SELECT weapon_names, gametime, clearancestatus, clearancetime FROM localmessage WHERE username = ?";
        List<Map<String, Object>> gameData = gameMessageJdbcTemplate.queryForList(queryGameDataSQL, username);

        if (gameData.isEmpty()) {
            throw new RuntimeException("Game data not found for user: " + username);
        }

        Map<String, Object> gameDataRow = gameData.get(0);

        // Use String.valueOf to handle nulls or Integer to String conversion
        String weaponNames = (String) gameDataRow.get("weapon_names");
        String gametime = String.valueOf(gameDataRow.get("gametime"));  // Ensure this is a String, even if the DB returns an Integer
        String clearancestatus = String.valueOf(gameDataRow.get("clearancestatus")); // Same for this
        String clearancetime = String.valueOf(gameDataRow.get("clearancetime")); // Same for this

        // Step 3: Query weapon levels for each weapon
        String[] weapons = weaponNames.split(",");
        StringBuilder weaponLevels = new StringBuilder();

        for (String weapon : weapons) {
            String queryWeaponSQL = "SELECT weapon_level FROM weapons_levels WHERE weapon_name = ?";
            List<Map<String, Object>> weaponResult = gameMessageJdbcTemplate.queryForList(queryWeaponSQL, weapon);

            if (!weaponResult.isEmpty()) {
                weaponLevels.append(weaponResult.get(0).get("weapon_level")).append(",");
            }
        }

        // Remove trailing comma
        if (weaponLevels.length() > 0) {
            weaponLevels.setLength(weaponLevels.length() - 1);
        }

        // Step 4: Create JSON response
        return Map.of(
                "gametime", gametime,
                "clearancestatus", clearancestatus,
                "clearancetime", clearancetime,
                "weapons", weaponNames,
                "weapon_levels", weaponLevels.toString()
        );
    }
}
